package com.crocodileQ.blog.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.crocodileQ.blog.domain.dto.BlogArticlesDto;
import com.crocodileQ.blog.domain.vo.BlogArticlesVo;
import com.crocodileQ.common.core.page.BlogTableData;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.crocodileQ.common.annotation.Log;
import com.crocodileQ.common.core.controller.BaseController;
import com.crocodileQ.common.core.domain.AjaxResult;
import com.crocodileQ.common.enums.BusinessType;
import com.crocodileQ.blog.domain.BlogArticles;
import com.crocodileQ.blog.service.IBlogArticlesService;
import com.crocodileQ.common.utils.poi.ExcelUtil;
import com.crocodileQ.common.core.page.TableDataInfo;

/**
 * 文章管理Controller
 * 
 * @author crocodilePole
 * @date 2025-04-10
 */
@RestController
@RequestMapping("/blog/blog_articles")
public class BlogArticlesController extends BaseController
{
    @Autowired
    private IBlogArticlesService blogArticlesService;

    /**
     * 查询文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:blog_articles:list')")
    @GetMapping("/list")
    public BlogTableData list(BlogArticlesDto blogArticlesDto)
    {
        startPage();
        List<BlogArticlesVo> list = blogArticlesService.selectBlogArticlesList(blogArticlesDto);
        //获取总记录、已发布、草稿、回收站信息
        if (list.isEmpty()) {
            // 如果列表为空，返回一个默认的 countMap 或抛出异常
            HashMap<String, Integer> countMap = new HashMap<>();
            countMap.put("all", 0); // 总记录数
            countMap.put("published", 0); // 已发布文章数
            countMap.put("draft", 0); // 草稿文章数
            countMap.put("recycle", 0); // 回收站文章数
            return getBlogDataTable(countMap, list);
        }

        // 获取总记录、已发布、草稿、回收站信息
        HashMap<String, Integer> countMap = blogArticlesService.countByUserId(list.get(0).getUserId());
        return getBlogDataTable(countMap, list);
    }

    /**
     * 导出文章管理列表
     */
    @PreAuthorize("@ss.hasPermi('blog:blog_articles:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogArticles blogArticles)
    {
        BlogArticlesDto blogArticlesDto = BeanUtil.copyProperties(blogArticles, BlogArticlesDto.class);
        List<BlogArticlesVo> list = blogArticlesService.selectBlogArticlesList(blogArticlesDto);
        ExcelUtil<BlogArticlesVo> util = new ExcelUtil<>(BlogArticlesVo.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('blog:blog_articles:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(blogArticlesService.selectBlogArticlesById(id));
    }

    /**
     * 新增文章管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blog_articles:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogArticlesDto blogArticlesDto)
    {
        return toAjax(blogArticlesService.insertBlogArticles(blogArticlesDto));
    }

    /**
     * 修改文章管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blog_articles:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogArticlesDto blogArticlesDto)
    {
        return toAjax(blogArticlesService.updateBlogArticles(blogArticlesDto));
    }

    /**
     * 删除文章管理
     */
    @PreAuthorize("@ss.hasPermi('blog:blog_articles:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(blogArticlesService.deleteBlogArticlesByIds(ids));
    }
}
